/**
 * 
 */
$(document).ready(function() {
		var investRemoteTrans = new Helper.RemoteTrans({
			url : Helper.basePath + "/investInfo/doInvest.htm",
			isAsync : false,
			onSuccess : function(jsonData) {
				if (jsonData.code != "000") {
					P2PWAP.ui.toast(jsonData.message);
				} else {
					if(jsonData.returnObject){
						var direc_page = window.open("", "direc_page");
						direc_page.document.write(jsonData.returnValue.orderFormContent);
					}else{
						alert(jsonData.returnValue.orderFormContent);
						location.reload(true);
					}
				}
			},
			onError : function(data) {
				$("#invest_err_msg").text(data.message);
				P2PWAP.ui.toast(data.message);
			}
		});
		//点击投资
		$("#JS-submit").click(function() {
			var msg = juegeMoney();
	        if (msg != true) {
	            P2PWAP.ui.toast(msg);
	            return;
	        }
			var money = $.trim($("#JS-money").val());
			if (money == null || money == "") {
				P2PWAP.ui.toast("请输入投资金额");
				return;
			}
			if (isNaN(money)) {
				P2PWAP.ui.toast("投资金额必须为数字");
				return;
			}
			var re = /^[1-9]\d*$/;
			if (!re.test(money)) {
				P2PWAP.ui.toast("投资金额必须为正整数!");
		        return;
		    }
			if(window.confirm('您确定要投资操作吗？')){
				investRemoteTrans.params = {borrowInfoId:$("#borrowInfoId").val().trim(),investMoney:$("#JS-money").val(),redPacketId:$("#chooseid").val()};
				investRemoteTrans.send();
			}
		});
		
});

function juegeMoney(){
    var money = $("#JS-money").val() * 1;
    var mini = $("#mini").val();
    var avaliable = $("#avaliable").val();
    var remain = $("remain").val();
    if (!(money > 0)) {
        return "请输入正确的金额";
    } else if (money < mini){
        return "起投金额为" + mini + "元";
    } else if (money > avaliable){
        return "该标剩余" + avaliable + "元";
    } else if (money > remain){
        return "可用余额为" + remain + "元";
    } else {
    	changeContraLink();
        return true;    
    }
}